package com.shanghaionstar.service;

import android.content.Context;

import com.onstar.cn.api.PublicService;
import com.onstar.cn.auth.CheckNewVersion;
import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.common.util.Constants;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.mag.enumtype.OSType;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import rx.Observable;
import rx.functions.Func0;

/**
 * Created by Rengui Lu on 2016/8/19.
 */
public class MainTabService<T> extends BaseService<T> {
    private Context context;

    public MainTabService(Context context){
        this.context = context;
    }



    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.GET)
    public String checkNewVersionJson() throws OnStarException{
        PublicService service = new PublicService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        service.appVersion(requestEntity, OSType.ANDROID.toString());
        requestEntity = HttpInjectUtils.injectHttp(getClass(), "checkNewVersionJson", requestEntity);
        return BusinessUtil.getResponse(getDatas(requestEntity), context);
    }

    public Observable<CheckNewVersion> checkNewVersion() {
        return Observable.defer(new Func0<Observable<CheckNewVersion>>() {
            @Override
            public Observable<CheckNewVersion> call() {
                try {
                    return Observable.just((CheckNewVersion) GsonHelper.fromJson(checkNewVersionJson(),CheckNewVersion.class));
                } catch (OnStarException e) {
                    return Observable.error(e);
                }
            }
        });
    }

//
    @Override
    public RequestEntity getRequestEntity() {
        return null;
    }

    @Override
    public T jsonToBean(String json) {
        return null;
    }
}
